﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace GraffitiExtras.Plugins.CommunityCredit
{
    internal class CcUtil
    {
        #region Public methods

        public static PointCategoryCollection GetCategoriesFromDataSet(DataSet dataSet)
        {
            PointCategoryCollection result = new PointCategoryCollection();

            foreach (DataTable table in dataSet.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    PointCategory category = new PointCategory();

                    category.ID = (int)row["PointCategoryID"];
                    category.Area = (string)row["PointAreas"];
                    category.Code = (string)row["PointCode"];
                    category.Value = (int)row["PointValue"];
                    category.Description = (string)row["PointDescription"];
                    category.DescriptionDisplay = (string)row["PointDescriptionDisplay"];
                    category.IsActive = ((string)row["IsActive"] == "Y") ? true : false;
                    category.IsIneta = ((string)row["IsIneta"] == "Y") ? true : false;
                    category.DisplayNamePlural = (string)row["DisplayNamePlural"];

                    result.Add(category);
                }
            }

            return result;
        }

        public static PrizeCollection GetPrizesFromDataSet(DataSet dataSet)
        {
            PrizeCollection result = new PrizeCollection();

            foreach (DataTable table in dataSet.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    Prize prize = new Prize();

                    prize.ID = (int)row["PrizeID"];
                    prize.Name = (string)row["PrizeName"];
                    prize.Subtitle = (string)row["PrizeSubtitle"];
                    prize.WinningPoints = (string)row["WinningPoints"];
                    prize.Description = (string)row["PrizeDescription"];
                    prize.Winner = (string)row["PrizeWinner"];
                    prize.ImagePath = (string)row["PrizeImagePath"];
                    prize.ThumbnailPath = (string)row["PrizeThumbnailPath"];
                    prize.ContestID = (int)row["PrizeContestID"];
                    prize.Type = (string)row["PrizeType"];
                    prize.Active = ((string)row["PrizeActive"] == "Y") ? true : false;
                    prize.DisplayOrder = (int)row["PrizeDisplayOrder"];
                    prize.Value = (decimal)row["PrizeValue"];
                    prize.Source = (string)row["PrizeSource"];
                    prize.Sent = ((string)row["PrizeSent"] == "Y") ? true : false;
                    if (!(row["PrizeSentDate"] is DBNull))
                        prize.SentDate = (DateTime)row["PrizeSentDate"];
                    prize.DeliveryNotes = (string)row["PrizeDeliveryNotes"];
                    prize.MailCode = (string)row["PrizeMailCode"];
                    prize.Ordered = ((string)row["PrizeOrdered"] == "Y") ? true : false;
                    prize.Delivered = ((string)row["PrizeDelivered"] == "Y") ? true : false;
                    prize.AwardDelivered = ((string)row["AwardDelivered"] == "Y") ? true : false;
                    if (!(row["WinnerCity"] is DBNull))
                        prize.WinnerCity = (string)row["WinnerCity"];
                    if (!(row["WinnerState"] is DBNull))
                        prize.WinnerState = (string)row["WinnerState"];
                    if (!(row["WinnerZip"] is DBNull))
                        prize.WinnerZip = (string)row["WinnerZip"];
                    if (!(row["WinnerCountry"] is DBNull))
                        prize.WinnerCountry = (string)row["WinnerCountry"];
                    if (!(row["WinnerAddress"] is DBNull))
                        prize.WinnerAddress = (string)row["WinnerAddress"];
                    if (!(row["DateOrdered"] is DBNull))
                        prize.DateOrdered = (DateTime)row["DateOrdered"];
                    if (!(row["DateSent"] is DBNull))
                        prize.DateSent = (DateTime)row["DateSent"];
                    if (!(row["DateReceived"] is DBNull))
                        prize.DateReceived = (DateTime)row["DateReceived"];
                    prize.Status = (string)row["Status"];
                    if (!(row["ShippingCode"] is DBNull))
                        prize.ShippingCode = (string)row["ShippingCode"];
                    if (!(row["Shipper"] is DBNull))
                        prize.Shipper = (string)row["Shipper"];
                    if (!(row["OrderNumber"] is DBNull))
                        prize.OrderNumber = (string)row["OrderNumber"];
                    prize.Invoiceable = ((string)row["Invoiceable"] == "Y") ? true : false;

                    result.Add(prize);
                }
            }

            return result;
        }

        public static DemographicCollection GetDemographicsFromDataSet(DataSet dataSet)
        {
            DemographicCollection result = new DemographicCollection();

            foreach (DataTable table in dataSet.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    Demographic demographic = new Demographic();

                    demographic.ID = (int)row["DemographicID"];
                    demographic.IPAddress = (string)row["IPAddress"];
                    demographic.XCoord = (string)row["XCoord"];
                    demographic.YCoord = (string)row["YCoord"];
                    demographic.City = (string)row["City"];
                    demographic.State = (string)row["State"]; ;
                    demographic.Country = (string)row["Country"];
                    demographic.Date = DateTime.Parse((string)row["DateEntered"]);

                    result.Add(demographic);
                }
            }

            return result;
        }

        public static EarnersCreditCollection GetEarnersCreditFromDataSet(DataSet dataSet)
        {
            EarnersCreditCollection result = new EarnersCreditCollection();

            foreach (DataTable table in dataSet.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    EarnersCredit credit = new EarnersCredit();

                    credit.ID = (int)row["CommunityCreditID"];
                    credit.Email = (string)row["Email"];
                    credit.Task = (string)row["Task"];
                    credit.ReferenceURL = (string)row["ReferenceURL"];
                    credit.Date = (DateTime)row["DateEarned"];
                    credit.Source = (string)row["Source"];
                    credit.Category = (string)row["Category"];
                    credit.Points = (int)row["Points"];
                    credit.Name = (string)row["Name"];
                    credit.PointsActive = (string)row["PointsActive"];
                    credit.DateAdded = (DateTime)row["DateAdded"];
                    credit.LastChangeBy = (string)row["LastChangeBy"];
                    credit.LastChangeDate = (DateTime)row["LastChangeDate"];

                    result.Add(credit);
                }
            }

            return result;
        }

        public static TopEarnerCollection GetTopEarnersFromDataSet(DataSet dataSet)
        {
            TopEarnerCollection result = new TopEarnerCollection();

            foreach (DataTable table in dataSet.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    TopEarner topEarner = new TopEarner();

                    topEarner.Name = (string)row["Name"];
                    topEarner.TotalPoints = (int)row["TotalPoints"];

                    if (row["HasHandicap"] != null)
                        topEarner.HasHandicap = ((string)row["HasHandicap"] == "Y") ? true : false;
                    else
                        topEarner.HasHandicap = false;

                    result.Add(topEarner);
                }
            }

            return result;
        }
        #endregion
    }
}
